package CompBlastMany;

use strict;

use lib 'lib';


use CompBlast;
use Bio::Seq;
use Bio::SeqIO;
use Bio::Perl;
use Term::ProgressBar;

sub new {
	my ($class,@genus) = @_;

	my $self = ();
	$self->{data} = undef;
	$self->{score} = undef;
	$self->{all} = 0;
	$self->{iter} = 4;
	$self->{info} = undef;
	$self->{verbose} = 0;

	bless $self, $class;
	return $self;
}

sub GetDescription {
	my ($self, $seq);

	my @info = @{$self->{info}};
	foreach(@info){
		my @i = @$_;
		return $i[1] if ($i[0] eq $seq->id);
	}

	return "";
}

sub SetAll {
	my ($self,$s) = @_;
	$self->{all} = $s;	
}

sub SetHits {
	my ($self,$h) = @_;
	$self->{iter} = $h;
}

sub GetData {
	my ($self) = shift;
	my @data = @{$self->{data}};
	@data = sort { $b->[1] <=> $a->[1] } @data;
	return @data;
}

sub SetVerbose {
	my ($self, $v) = @_;
	$self->{verbose} = $v;
}


sub CheckFile {
	my ($self, $filename, @keywords) = @_;
	my $iter = $self->{iter};
	my $tmp = Bio::SeqIO->new(-format=>'fasta',-file=>"$filename");
	my $seqs = Bio::SeqIO->new(-format=>'fasta',-file=>"$filename");
	my $factory = new CompBlast($iter,@keywords);	
	my @data = ();
	my @info = ();
	my $v = $self->{verbose};

	my $count = 0;
	while(my $seq = $tmp->next_seq()){
		$count++
	}
	my $j = 0;
	my $progress = Term::ProgressBar->new({count => $count,ETA => 'linear', name => 'Blasting and comparing'});
	$progress->minor(0);
	#print sprintf( "%2.0f%%",0);
	while(my $seq = $seqs->next_seq()){
	#	print "\b\b\b" . sprintf( "%2.0f%%",$j/$count*100);
		
	#	print $seq->id . "\n" if($v > 0);
		my ($i,$q) = $factory->Check($seq, $self->{all}, $v);
		push @info,$i;	
		#print "\tScore: " . $q . "\n" if($v > 0);
		push @data, [ ($seq->id, $q)  ];
		$j++;
		$progress->update($j);
	}
	$progress->update($count);
	
	$self->{info} = [ @info ];
	$self->{data} = [ @data ];
	return 1;
}

1;
